package com.relytosoft.mizar.media.analyze.services.service.system.impl;

import cn.hutool.core.util.StrUtil;
import com.relytosoft.dubhe.common.api.entity.system.SysFile;
import com.relytosoft.dubhe.common.api.service.RemoteFileService;
import com.relytosoft.mizar.media.analyze.services.service.system.AyUploadImageService;
import com.relytosoft.mizar.media.common.constant.SymbolConstants;
import com.relytosoft.mizar.media.common.utils.ImageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author wss
 * @version 1.0
 * @date 2022/5/20 14:07
 */
@Service
public class AyUploadImageServiceImpl implements AyUploadImageService {

    @Autowired
    RemoteFileService remoteFileService;

    @Override
    public String base64Upload(String base64Str, Integer upLoadFileCode) {
        SysFile upload = remoteFileService
                .upload(null, null, null, ImageUtil.GenerateImage(base64Str), upLoadFileCode);
        return upload.getUrl();
    }

    @Override
    public String base64FrameUpload(String base64Str, int leftBottomX, int leftBottomY, int rightTopX, int rightTopY, Integer upLoadFileCode) {
        byte[] bytes = ImageUtil.imageFrame(ImageUtil.GenerateImage(base64Str),
                leftBottomX, leftBottomY, rightTopX, rightTopY
        );
        SysFile upload = remoteFileService
                .upload(null, null, null, bytes, upLoadFileCode);
        return upload.getUrl();
    }

    @Override
    public String rawUrlUpload(String imageUrl, Integer upLoadFileCode) {
        String actUrl = StrUtil.sub(imageUrl, imageUrl.lastIndexOf(SymbolConstants.EQ) + 1, imageUrl.length());
        SysFile upload = remoteFileService
                .upload(null, null, null, ImageUtil.urlToBytes(actUrl), upLoadFileCode);
        return upload.getUrl();
    }

    @Override
    public String rawUrlFrameUpload(String imageUrl, int leftBottomX, int leftBottomY, int rightTopX, int rightTopY, Integer upLoadFileCode) {
        String actUrl = StrUtil.sub(imageUrl, imageUrl.lastIndexOf(SymbolConstants.EQ) + 1, imageUrl.length());
        byte[] bytes = ImageUtil.imageFrame(ImageUtil.urlToBytes(actUrl),
                leftBottomX, leftBottomY, rightTopX, rightTopY
        );
        SysFile upload = remoteFileService
                .upload(null, null, null, bytes, upLoadFileCode);
        return upload.getUrl();
    }
}
